import json

from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
# from rest_framework.response import Response
# from django.contrib.auth.models import User
from follow_sys.models import *
# from .models import *
from account_sys.models import User
from enterprise_sys.models import *
from utils.response_utils import build_response, method_disallowed_response
from utils.session_utils import has_login, get_login_user

@csrf_exempt
@api_view(['POST'])
def follow_user(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)
    data = request.data
    followee_user_id = data.get('followee_user_id')

    try:
        followee_user = User.objects.get(id =followee_user_id)
    except User.DoesNotExist:
        return build_response(errno=4004, msg='被关注用户不存在')

    if Follow_user.objects.filter(follower=user, followee_user=followee_user).exists():
        return build_response(errno=4005, msg='已关注该用户')

    Follow_user.objects.create(follower=user, followee_user=followee_user)

    return build_response(msg='关注成功')

@csrf_exempt
@api_view(['POST'])
def follow_enterprise(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)
    data = request.data
    followee_enterprise_id = data.get('followee_enterprise_id')

    try:
        followee_enterprise = Enterprise.objects.get(id=followee_enterprise_id)
    except Enterprise.DoesNotExist:
        return build_response(errno=4004, msg='被关注企业不存在')

    if Follow_enterprise.objects.filter(follower=user, followee_enterprise=followee_enterprise).exists():
        return build_response(errno=4005, msg='已关注该企业')

    Follow_enterprise.objects.create(follower=user, followee_enterprise=followee_enterprise)

    return build_response(msg='关注成功')

@csrf_exempt
def count_follow(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')
    if has_login(request):
        user: User = get_login_user(request)
    else:
        return build_response(errno=2001, msg='用户未登录')
    targetUserId = request.GET.get('targetUserId')
    count_follows = Follow_user.objects.filter(followee_user_id=targetUserId)
    return build_response({
        "code":200,
        "msg": count_follows.count()
    })


@csrf_exempt
def unfollowUser(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')
    if has_login(request):
        user: User = get_login_user(request)
    else:
        return build_response(errno=2001, msg='用户未登录')
    data = json.loads(request.body)
    followedId = data.get('followedId')
    follows = Follow_user.objects.filter(followee_user_id=followedId,follower_id=user.id)
    if follows.count() == 0:
        return build_response(errno=2001, msg='未关注该用户')

    follows[0].delete()
    return build_response(msg='取消关注成功')

@csrf_exempt
def unfollowEnterprise(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')
    if has_login(request):
        user: User = get_login_user(request)
    else:
        return build_response(errno=2001, msg='用户未登录')
    data = json.loads(request.body)
    followedId = data.get('followedId')
    follows = Follow_enterprise.objects.filter(followee_enterprise_id=followedId,follower_id=user.id)
    if follows.count() == 0:
        return build_response(errno=2001, msg='未关注该企业')

    follows[0].delete()
    return build_response(msg='取消关注成功')


@csrf_exempt
def verify_follow(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')
    if has_login(request):
        user: User = get_login_user(request)
    else:
        return build_response(errno=2001, msg='用户未登录')
    targetUserId = request.GET.get('followedUserId')
    userId = request.GET.get('followerUserId')

    count_follows = Follow_user.objects.filter(followee_user_id=targetUserId,follower_id=userId)
    if count_follows.count() == 0:
        return build_response({
            "code": 200,
            "msg": False
        })

    return build_response({
        "code":200,
        "msg": True
    })

